/**
 * 全站权限配置
 *
 */
 import router from './router/router'
 import store from '@/store'
 import { validatenull } from '@/util/validate'
 import NProgress from 'nprogress' // progress bar
 import 'nprogress/nprogress.css' // progress bar style
 import {loginByMobile1} from '@/api/login'
 NProgress.configure({ showSpinner: false })
 const lockPage = store.getters.website.lockPage // 锁屏页
 router.beforeEach((to, from, next) => {
   // 获取应用名称
   store.dispatch("getPublic");
   // 缓冲设置
   if (to.meta.keepAlive === true && store.state.tags.tagList.some(ele => {
     return ele.value === to.fullPath
   })) {
     to.meta.$keepAlive = true
   } else {
     NProgress.start()
     if (to.meta.keepAlive === true && validatenull(to.meta.$keepAlive)) {
       to.meta.$keepAlive = true
     } else {
       to.meta.$keepAlive = false
     }
   }
   const meta = to.meta || {}
   if (store.getters.access_token) {
     if (store.getters.isLock && to.path !== lockPage) {
       next({ path: lockPage })
     } else if (to.path === '/login') {
       next({ path: '/' })
     } else {
       if (store.getters.roles.length === 0) {
         store.dispatch('GetUserInfo').then(() => {
           next()
         }).catch(() => {
           store.dispatch('FedLogOut').then(() => {
             next({ path: '/login' })
           })
         })
       } else {
         const value = to.query.src || to.fullPath
         const label = to.query.name || to.name
         if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
           store.commit('ADD_TAG', {
             label: label,
             value: value,
             params: to.params,
             query: to.query,
             title: to.params.newtitle,
             group: router.$avueRouter.group || []
           })
         }
         next()
       }
     }
   } else {
     if (meta.isAuth === false) {
       next()
     } else {
       if (to.query.mobile) {
         store.dispatch("LoginByPhone1", to.query).then(() => {
          store.dispatch("GetUserInfo").then((res) => {
            next()
          });
         })
       } else {
        next('/login')
       }
     }
   }
 })
 
 router.afterEach(() => {
   NProgress.done()
   const title = store.getters.tag.label
   router.$avueRouter.setTitle(title)
 })
 